import Vue from 'vue'
import Vuex from 'vuex'
import { getDateStrFormate1,getDateStrFormate } from "../components/common.js";

Vue.use(Vuex)

const monthDayMap = {
    "0":31,
    "1":28,
    "2":31,
    "3":30,
    "4":31,
    "5":30,
    "6":31,
    "7":31,
    "8":30,
    "9":31,
    "10":30,
    "11":31
}

// 计算借款天数
function getLoanDays (statementDay,today) { //  statementDay账单日 today服务器时间
    var loanDays = 0;
    var nowDate = today.getDate(); // new Date() 到时用today替代
    if((statementDay-nowDate)>-1){  // 单月账单的借款天数
        loanDays = statementDay-nowDate + 1;
//        setAccountRepaymentDate(today,0,statementDay);
    }else{  // 跨月的借款天数
        var nowMonth = today.getMonth();
        var monthDays = monthDayMap[nowMonth];
        loanDays = 1 + statementDay + monthDays - nowDate;
//        setAccountRepaymentDate(today,1,statementDay);
    }

    return loanDays;
}

/*// 帐期还款日
function setAccountRepaymentDate(today,addMonth,day) {
    var rpDate = new Date(today.getTime());
    rpDate.setMonth(rpDate.getMonth() + addMonth);
    rpDate.setDate(day);
    state.repaymentDate = getDateStrFormate("yyyy-mm-dd",rpDate);
}*/

const state = {
  /**********共用start*****************************/
    showLoading:false,
    upLimit:"50000",
    lowLimit:"600",
  /**********共用start*****************************/
    headerTitle:"",
    test:"",
    errorMsg:"",
    openid:"",  //
    cardId:"",                     //信用卡号共用的
    prdtNo:"0301",             //0301:闪贷产品号
    channelNo:"01",            //01 产品渠道  02 帐期渠道
    tlMerId:"",     //通联商户号
    nowDate:new Date(),            //当前日期时间
    userName:"",   //
    interestRate:"",           //日利率
    coverCharge:"",              //1% 的手续费
    loanApplyMoney:"",             //闪贷借款金额
    repaymentDate:"",              //闪贷最后还款日  帐期还款日
    dailyInterest:"0.00",          //日利息
    maximumRepay:"0.00",           //最大还款额 冻结资金
    srDateId: "statementDate",     //帐期 statementDate 账单日    repaymentDate 还款日
    accountLoanAmount:"",        //帐期借款金额
    accountServiceCharge:"0.00",   //账期手续费
    accountRepaymentAmount: "0.00",//账期还款额
    accountreMonthpaymentDate:"",       //信用还款日1-30 数字
    accountRepaymentDate: "",      //账期还日
    statementDate:"",              //账期信用卡账单日
    accountLoanDays:"",            //帐期借款天数算利息的天数
    loanDays:"",                   // 帐期借款天数 给用看的天数
    accountChecked:true,           //帐期勾选协议
    accountNoToinfo:true,         //帐期是否允许到填写信息界面
    payCode:"031509694477939740", //付款码
    /************订单详情对象*********************/
    detailItem:"",
    /**********************交易结果************/
    resultCode:"2",                //交易结果1：成功  其它|2：失败
    result_msg2:"",
    /*************提前还款start对象**************************/
    repayItem:"",
    /*************提前还款end**************************/
    showBack:false,   // 是否显示返回
    /*************打包**************************/
    imagesUrl:"/FuLiBao/Lenovo/static/images/", // 打包时打开
//   imagesUrl:"/static/images/",     // 开发地址
}

const mutations = {
    changeTitle(state,htitle){
        state.headerTitle = htitle;
    },
    changeInputName(state,nameValue){
        state.inputName = nameValue;
    },
    changeinputPhone(state,phoneValue){
        state.inputPhone = phoneValue;
    },
    accountRepaymentAmountFunc(){  // 计算帐期还款额 和手续费
        if(state.statementDate != "" && state.accountLoanAmount != ""){

            state.accountServiceCharge = (parseFloat(state.accountLoanAmount) * (parseFloat(state.interestRate) * state.accountLoanDays +  state.coverCharge)+0.0000001).toFixed(2);
            state.accountRepaymentAmount = (parseFloat(state.accountLoanAmount) * (parseFloat(state.interestRate) * state.accountLoanDays +  state.coverCharge + 1)+0.0000001).toFixed(2);
            state.dailyInterest = (parseFloat(state.accountLoanAmount) * parseFloat(state.interestRate)).toFixed(2);
        }else{
           state.accountRepaymentAmount = "0.00";
           state.accountServiceCharge = "0.00";
        }
    },
    accountLoanDaysFunc(){  // 计算帐期借款天数
        state.accountLoanDays = getLoanDays(parseInt(state.statementDate),new Date());
        mutations.changeNoToinfo();
    },
    changeNoToinfo(){
        if(state.accountLoanAmount>=state.lowLimit && state.accountLoanAmount<= state.upLimit && state.accountChecked && state.accountLoanDays != "" && state.accountreMonthpaymentDate != "" && state.statementDate !=""){
            state.accountNoToinfo = false;
        }else{
            state.accountNoToinfo = true;
        }
    },
    setNowDate(state,dateStr){
        state.nowDate = new Date();
        $.post(XtsdOrderServlet,{"servlet_type":"query_today"},function(dmsg) {
            if(dmsg.today_response && dmsg.today_response.today){
                var nowDateStr = getDateStrFormate1(dmsg.today_response.today,"date") + " 00:00:01";
                state.nowDate = new Date(nowDateStr.replace(/-/g, "/"));
            }
        });
    },
    getRate(){  // 取利率和服务费
        var rparaments = {
            prdt_no:state.prdtNo,
            channel_no:state.channelNo,
            servlet_type:"query_mer_info"
        }
        $.post(XtsdOrderServlet,rparaments,function(rdata) { // 取日利率和 服务费率
            console.dir(rdata);
            if(rdata.xtsdprdtrate_response && rdata.xtsdprdtrate_response.dayRate){
                state.interestRate = parseFloat(rdata.xtsdprdtrate_response.dayRate)/100;
                state.coverCharge = parseFloat(rdata.xtsdprdtrate_response.serviceRate)/100;
            }
        })
    },
    getLimit(cNo){
        var paraments ={
            merNo:state.tlMerId,
            prdtNo:state.prdtNo,
            channelNo:state.channelNo,
            servlet_type:"query_prdt_mult"
        }
        $.post(XtsdOrderServlet,paraments,function (lmdata) {
            console.dir(lmdata);
            if(lmdata.xtsd_PrdtMulRate_response && lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays && lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays.merPrdtMultRates && lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays.merPrdtMultRates[0] && lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays.merPrdtMultRates[0].amtUplimit){
                state.upLimit = lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays.merPrdtMultRates[0].amtUplimit;
                state.lowLimit = lmdata.xtsd_PrdtMulRate_response.merPrdtMultRates_arrays.merPrdtMultRates[0].amtLowlimit;
            }

        })

    }
}

export default new Vuex.Store({
    state,
    mutations
})
